<template>
    <div style="width:100%;height:100%;overflow:hidden">
        <weui-cell style="overflow-y:auto"
                   :style="{maxHeight: 'calc(100% - 0px)'}" :list="cell_0_list">
        </weui-cell>
    </div>
</template>

<script>
    import WeuiCell from '../vue2componentSet/src/components/WeuiCell'
    import global from '../lib/global'
    import axios from 'axios'

    export default {
        components: {
            WeuiCell
        },
        data() {
            return {
                bills: []
            }
        },
        mounted() {
            this.$store.commit('set_title', '返现记录')

            if (!this.$store.state.customer) {
                this.$store.commit('login_dialog', {
                    is_show: true,
                    resolve: () => {
                        this.$router.push('/returnRecord')
                    }
                })
                this.$router.goback()
                return
            }

            this.getBill(1)
        },
        computed: {
            cell_0_list() {
                var list = []
                this.bills.forEach(item => {
                    if (!item.returnTime) {
                        return
                    }
                    var year_month = item.returnTime.substring(0, 7)
                    if (list.length == 0) {
                        list.push({
                            value: year_month,
                            desc: parseFloat(item.returnMoney)
                        })
                        return
                    }
                    var obj
                    for (var i = 0; i < list.length; i++) {
                        if (list[i].value == year_month) {
                            obj = list[i]
                            obj.desc += parseFloat(item.returnMoney)
                            return
                        }
                        if (list[i].value > year_month) {
                            list.splice(i, 0, {
                                value: year_month,
                                desc: parseFloat(item.returnMoney)
                            })
                            return
                        }
                    }
                })
                list.forEach(item => {
                    item.desc = item.desc.toFixed(2)
                })
                return list
            }
        },
        methods: {
            getBill(page) {
                axios.post(global.server + 'getBill.do', {
                    page: page
                }).then(result => {
                    var obj = result.data
                    if (obj.result == 1) {
                        this.bills.push.apply(this.bills, obj.bills)
                        if (obj.bills.length >= 10) {
                            this.getBill(page + 1)
                        }
                    } else if (obj.result == 0) {
                        this.$store.commit('customer_login', null)
                        this.$router.goback()
                    }
                })
            }
        }
    }
</script>
